package com.zyw.dev.zyw.db;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.zyw.dev.zyw.MyApplication;
import com.zyw.dev.zyw.dao.DaoMaster;
import com.zyw.dev.zyw.dao.DaoSession;
import com.zyw.dev.zyw.dao.ZJWBeans;
import com.zyw.dev.zyw.dao.ZJWBeansDao;

import java.util.List;

/**
 * Created by misl on 16/8/3.
 */
public class DataBaseHealper {

    public static DataBaseHealper self;

    private DaoMaster.DevOpenHelper devOpenHelper;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private ZJWBeansDao zjwBeansDao;

    public static DataBaseHealper newInstance() {
        synchronized (DataBaseHealper.class){
            if (null ==  self){
                self = new DataBaseHealper();
            }
        }
        return self;
    }

    public void initDataBase(){
        devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.newInstance().getApplicationContext(), "ZJWBeans-db", null);
        daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        daoSession = daoMaster.newSession();
        zjwBeansDao = daoSession.getZJWBeansDao();
    }

    public void remove(){

    }

    public long insert(ZJWBeans zjwBeans){
        if (null != zjwBeansDao){
           return zjwBeansDao.insert(zjwBeans);
        }
        return -1;
    }

    public void delete(ZJWBeans zjwBeans){
        if (null != zjwBeansDao){
            zjwBeansDao.delete(zjwBeans);
        }
    }

    public List<ZJWBeans> read(){
        if (null == zjwBeansDao) {
            return null;
        }
        return zjwBeansDao.queryBuilder().where(ZJWBeansDao.Properties.Id.notEq(999)).orderDesc(ZJWBeansDao.Properties.Id).build().list();
    }
}
